package com.easylinkin.linkappapi.lobar.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.common.exceptions.BusinessException;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.lobar.entity.LobarSet;
import com.easylinkin.linkappapi.lobar.mapper.LobarSetMapper;
import com.easylinkin.linkappapi.lobar.service.LobarSetService;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 劳务设置表 服务实现类
 * </p>
 *
 * @author TongJie
 * @since 2022-04-13
 */
@Service
public class LobarSetServiceImpl extends ServiceImpl<LobarSetMapper, LobarSet> implements
    LobarSetService {

  @Resource
  private LinkappUserContextProducer linkappUserContextProducer;

  /**
   * 增加
   * @param lobarSet
   */
  @Override
  public void insert(LobarSet lobarSet) {
    /**
     * 验证重复
     */
//    this.checkExist(lobarSet);

    //查询本项目是否已设置
    String tenantId = linkappUserContextProducer.getNotNullCurrent().getTenantId();
    QueryWrapper<LobarSet> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("tenant_id_",tenantId);
    List<LobarSet> lobarSets = baseMapper.selectList(queryWrapper);
    if (lobarSets.size() > 0){
      lobarSet.setId(lobarSets.get(0).getId());
    }else {
      lobarSet.setId(null);
    }
    //设置基本属性
    this.setBase(lobarSet);
    if (null != lobarSet.getId()){
      this.updateById(lobarSet);
    }else {
      this.save(lobarSet);
    }
  }

  /**
   * 根据id编辑
   * @param lobarSet
   */
  @Override
  public void updateOne(LobarSet lobarSet) {
    /**
     * 验证重复
     */
    this.checkExist(lobarSet);
    //设置基本属性
    this.setBase(lobarSet);
    this.updateById(lobarSet);
  }

  /**
   * 查询该项目属于哪种考勤在场判断
   * @param tenantId
   * @return
   */
  @Override
  public LobarSet getByTenantId(String tenantId) {
    QueryWrapper<LobarSet> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("tenant_id_",tenantId);
    List<LobarSet> lobarSets = baseMapper.selectList(queryWrapper);
    if (lobarSets.size() > 0){
      return  lobarSets.get(0);
    }else {
      QueryWrapper<LobarSet> queryWrapper2 = new QueryWrapper<>();
      queryWrapper.isNull("tenant_id_");
      List<LobarSet> sets = baseMapper.selectList(queryWrapper2);
      return sets.get(0);
    }
  }

  /**
   * @Description: 获取当前项目配置
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @Override
  public LobarSet findOne() {
    String tenantId = linkappUserContextProducer.getNotNullCurrent().getTenantId();
    QueryWrapper<LobarSet> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("tenant_id_",tenantId).or().isNull("tenant_id_");
    List<LobarSet> lobarSets = baseMapper.selectList(queryWrapper);
    if (lobarSets.size() > 0){
      List<LobarSet> sets = lobarSets.stream().filter(l -> tenantId.equals(l.getTenantId()))
          .collect(Collectors.toList());
      if (sets.size() > 0){
        return sets.get(0);
      }else {
        return lobarSets.get(0);
      }
    }
    return null;
  }

  @Override
  public IPage<LobarSet> queryListByPage(RequestModel<LobarSet> requestModel) {
    Page page = requestModel.getPage();
    LobarSet lobarSet = requestModel.getCustomQueryParams();
    //项目id 过滤
    lobarSet.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
//    IPage<LobarSet> lobarSetIPage = baseMapper.queryListByPage(page, lobarSet);
    return null;
  }

  /**
   * 验证重复
   */
  private void checkExist(LobarSet lobarSet) {
    QueryWrapper<LobarSet> queryWrapper = new QueryWrapper<>();
    //todo 设置判断重复条件
    //编辑的时候存在id
    Optional.ofNullable(lobarSet.getId()).ifPresent(id -> queryWrapper.ne("id",lobarSet.getId()));
    Integer integer = baseMapper.selectCount(queryWrapper);
    if (integer>0){
      throw new BusinessException("该劳务设置已存在");
    }
  }

  /**
   * 设置基本属性
   * @param lobarSet
   */
  private void setBase(LobarSet lobarSet) {
    lobarSet.setModifyTime(new Date());
    if(null != linkappUserContextProducer.getCurrent()){
      lobarSet.setModifyId(linkappUserContextProducer.getCurrent().getId());
    }
    //没有id就是新增,有就是编辑
    if (null == lobarSet.getId()){
      if(null != linkappUserContextProducer.getCurrent()){
        lobarSet.setCreatorId(linkappUserContextProducer.getCurrent().getId());
      }
      lobarSet.setCreateTime(new Date());
      if(StringUtils.isBlank(lobarSet.getTenantId())){
        lobarSet.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
      }
    }
  }
}
